/***************************************************************************************************
 *
 * 
 *
 ***************************************************************************************************/

/**
 *
 *
 */
function category_waiting_request(inc)
{
	CATEGORY_REQUEST += (inc) ? 1 : (-1);
	$('category_working').display = (CATEGORY_REQUEST > 0) ? '' : 'none';
}

/**
 *
 *
 */
function categories_txt_init(tx,item)
{
	tx.type = 'text';
	tx.value = item.title;
	tx.name = item.id;

	tx.onclick = function(){	
			swap_editable(tx,true)
			tx.old_content = tx.value;
		};

	tx.onblur  = function(){
			swap_editable(tx,false)
			if(tx.old_content == tx.value) return;

			category_waiting_request(true);
			var url = 'index.php?c=ajax_categories&a=category_edit&cat_id=' + tx.name + '&title=' + encodeURIComponent(tx.value); 
			new Ajax.Request(url,{
				method: 'post',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					category_waiting_request(false);
				}
			});
		};

	swap_editable(tx,false);
}

/**
 *
 *
 */
function categories_chk_init(ch,item)
{
	ch.type = 'checkbox';
	ch.name = item.id;
	ch.checked = item.checked;
	ch.onclick = function(){	
		category_waiting_request(true);
		var url = 'index.php?c=ajax_categories&a=' + ((ch.checked) ? 'category_add' : 'category_del') + '&type=' + ITEM_TYPE +'&itm_id=' + ITEM_ID + '&cat_id='+ ch.name ; 
		new Ajax.Request(url,{
				method: 'get',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					category_waiting_request(false);
				}
			});
		}
}

/**
 *
 *
 */
function categories_refresh(categories)
{
	var ul = $('categories_item_list');
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	var tx = document.createElement('input');
	tx.value = 'Nueva Categoria';
	tx.className = 'txt edit_mode';
	tx.style.margin = '5px';
	tx.onblur  = function(){
			if(!confirm('Agregar Categoria?')) return;
			category_waiting_request(true);
			var url = 'index.php?c=ajax_categories&a=category_create&type=' + ITEM_TYPE + '&img_id=' + tx.name + '&title=' + encodeURIComponent(tx.value); 
			new Ajax.Request(url,{
				method: 'get',
				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					images_waiting_request(false);
					categories_load(ITEM_ID);
				}
			});
	};

	var li = document.createElement('li');
	li.appendChild(tx);
	ul.appendChild(li);

	categories.each(function(item){
		var li = document.createElement('li');
		var ch = document.createElement('input');
		var tx = document.createElement('input');
		categories_txt_init(tx,item);
		categories_chk_init(ch,item);
		li.appendChild(ch);
		li.appendChild(tx);
		ul.appendChild(li);
	})


}

/**
 *
 *
 */
function categories_load(item_id)
{
	category_waiting_request(true);
	new Ajax.Request('index.php?c=ajax_categories&a=categories&type='+ ITEM_TYPE +'&id='+ITEM_ID, {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			categories_refresh(json);
			category_waiting_request(false);
   	}
	});
}

function categories_load_all()
{
	new Ajax.Request('index.php?c=ajax_categories&a=categories_get_all', {
  	method:'get',
  	onSuccess: function(transport){
     	var json = transport.responseText.evalJSON();
			categories_list_refresh(json);
   	}
	});
}

function categories_init()
{
	categories_load_all();
}

/**
 *
 *
 */
function categories_list_refresh(items)
{
	// Sortable.destroy('item_list');

	var ul = $('categories_list');
	var lis = $A(ul.childNodes);
	lis.each(function(item){
			ul.removeChild(item);
			item = null;
		}
	);

	items.each(function(item){

    var im = document.createElement('img');
    im.src = '../public/icons/16x16/apps/delete.png';
		im.onclick = function(){
			if(confirm('Eliminar la categoria ' + item.title))
			{
				new Ajax.Request('index.php?c=ajax_categories&a=categories_delete&id=' + li.name, {
  				method:'get',
  				onSuccess: function(transport){
     			var json = transport.responseText.evalJSON();
					display_success_message(json);
					categories_load_all();
					}
				});
			}
		}

		var tx = document.createElement('input');
		categories_txt_init(tx,item);

		var li = document.createElement('li');
		li.appendChild(im);
		li.appendChild(tx);
		li.name = item.id;
		ul.appendChild(li);
	})
}


